﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ATest.Bll.Repositories;

namespace ATest.Tests.RepositoryTests
{
    [TestClass]
    public class QuestionRepositoryTest
    {
        private IQuestionRepository _QuestionRepository;
        private List<MockObjects.MockTest> _TestData;
        private List<MockObjects.MockQuestion> _QuestionData;

        [TestInitialize]
        public void Init()
        {
            //create 2 tests and 15 questions
            _TestData = MockObjects.MockTest.CreateList(2);
            _QuestionData = MockObjects.MockQuestion.CreateList(15);
            //assign the questions to a tests question[0] to test[0] question[1] to test[1] question[2] to test[0] etc
            _TestData.ForEach(test => test.SetQuestions(_QuestionData.Where((question, i) => i % 2 == _TestData.IndexOf(test)).Select(question => question.Object)));
            _QuestionData.ForEach(question => question.SetTest(_TestData[_QuestionData.IndexOf(question) % 2].Object));

            _QuestionRepository = new QuestionRepository(_QuestionData.Select(x => x.Object).AsQueryable());
        }

        [TestMethod]
        public void GetById_WithInvalidId_ReturnsNull()
        {
            var question = _QuestionRepository.GetById(int.MaxValue);
            Assert.IsNull(question);
        }

        [TestMethod]
        public void GetById_WithValidId_ReturnsCorrectQuestion()
        {
            var question = _QuestionRepository.GetById(_QuestionData[5].Object.Id);
            Assert.IsNotNull(question);
            Assert.AreEqual(_QuestionData[5].Object, question);
        }

        [TestMethod]
        public void GetAllByTestId_WithInvalidId_ReturnsEmptyList()
        {
            var questions = _QuestionRepository.GetAllByTestId(int.MaxValue);
            Assert.AreEqual(0, questions.Count());
        }

        [TestMethod]
        public void GetAllByTestId_WithValidId_ReturnsPopulatedList()
        {
            var questions = _QuestionRepository.GetAllByTestId(_TestData[1].Object.Id);
            Assert.AreEqual(_TestData[1].Object.Questions.Count, questions.Count());
        }
    }
}
